<?php
namespace app\forum\service;
/**
 * 标签接口
 */
class LabelService{

    /**
     * 调用边栏模块
     */
    public function sidebarList(){
        $where=array();
        $list = target('forum/Sidebar')->loadList($where = array());
        foreach ($list as $key => $value) {
            if($value['type']){
                $var = explode('_', $value['content'], 2);
                $content = service($var[0],'Sidebar','getForumSidebar');
                $list[$key]['content'] = $content[$var[1]]['content'];
            }
        }
        return $list;
    }

    /**
     * 调用菜单模块
     */
    public function menuList($data){
        $where=array();
        if(isset($data['position'])){
            $where[] = 'position in ('.$data['position'].')';
        }
        $list = target('forum/Menu')->loadList($where);
        foreach ($list as $key => $value) {
            $var = explode('_', $value['url'], 2);
            if($value['type']){
                if($var[0] == 'pages'){
                    $list[$key]['url'] = url('forum/Page',array('page_id'=>$var[1]));
                }else{
                    $content = service($var[0],'Menu','getForumMenu');
                    $list[$key]['url'] = url($content[$var[1]]['url']);
                    $list[$key]['icon'] = $content[$var[1]]['icon'];
                }
            }
        }
        return $list;
    }

	/**
	 * 板块列表
	 */
	public function plateList($data){
        $where= array();
        //上级板块
        if(isset($data['parent_id'])){
            $where['parent_id'] = $data['parent_id'];
        }
        //指定板块
        if(!empty($data['plate_id'])){
            $where[] = 'plate_id in ('.$data['plate_id'].')';
        }
        //板块属性
        if(isset($data['type'])){
            if($data['type']){
                $where['type'] = 1;
            }else{
                $where['type'] = 0;
            }
        }
        //其他条件
        if(!empty($data['where'])){
            $where[] = $data['where'];
        }
        //排序
        if(!empty($data['order'])){
            $order=$data['order'];
        }
        //其他属性
        $model = target('forum/Plate');
        return $model->loadData($where,$data['limit'],$order);
	}

    /**
     * 统计数量
     */
    public function threadCount($data){
        $where=array();
        //指定板块
        if(!empty($data['plate_id'])){
            $plateWhere = 'A.plate_id in ('.$data['plate_id'].')';
        }
        //指定板块下子板块帖子
        if ($data['sub']&&!empty($data['plate_id'])) {
            $plateIds = target('forum/Plate')->getSubClassId($data['plate_id']);
            $plateWhere = '';
            if(!empty($plateIds)){
                $plateWhere = "A.plate_id in ({$plateIds})";
            }
        }
        //其他条件
        if (!empty($data['where'])) {
            $where[] = $data['where'];
        }
        //其他属性
        $where['A.status'] = 1;
        return target('forum/Thread')->countList($where);
    }

    /**
     * 帖子列表
     */
    public function threadList($data){
        $where=array();
        //指定板块帖子
        if(!empty($data['plate_id'])){
            $plateWhere = 'A.plate_id in ('.$data['plate_id'].')';
        }
        //指定板块下子板块帖子
        if ($data['sub']&&!empty($data['plate_id'])) {
            $plateIds = target('forum/Plate')->getSubClassId($data['plate_id']);
            $plateWhere = '';
            if(!empty($plateIds)){
                $plateWhere = "A.plate_id in ({$plateIds})";
            }
        }
        if(!empty($plateWhere)){
            $where[] = $plateWhere;
        }
        //是否带形象图
        if (isset($data['image'])) {
            if($data['image'] == true)
            {
                $where[] = 'A.image <> ""';
            }else{
                $where['A.image'] = '';
            }
        }
        //调用APP帖子
        if(!empty($data['module'])){
            $where['B.app'] = $data['module'];
        }
        //排除ID
        if(!empty($data['not_id'])){
            $where[] = 'A.thread_id not in('.$data['not_id'].')';
        }
        //调用推荐位
        if(!empty($data['pos_id'])){
            $where[] = 'find_in_set('.$data['pos_id'].',A.position) ';
        }
        //其他条件
        if (!empty($data['where'])) {
            $where[] = $data['where'];
        }
        //调用数量
        if (empty($data['limit'])) {
            $data['limit'] = 10;
        }
        //帖子排序
        if(empty($data['order'])){
            $data['order']='A.post_time DESC,A.thread_id DESC';
        }
        //其他属性
        $where['A.status'] = 1;
        return target('forum/Thread')->loadList($where,$data['limit'],$data['order']);
    }

}
